<?php
$sub_menu = "230460";
include_once("_common.php");

auth_check($auth[$sub_menu], "r");

$token = get_token();

$qstr .= "&fr_date=$fr_date&to_date=$to_date&date=$date&mi_day=$mi_day&acc=$acc&ms_id=$ms_id";

if (!is_numeric($page)) $page = 1;

$sql_common = " from $mw_cash[membership_log_table] as l, $g4[member_table] as m ";
$sql_search = " where l.mb_id = m.mb_id ";

if ($ms_id) {
    $sql_search .= " and ms_id = '$ms_id' ";
    $row = sql_fetch(" select ms_name from $mw_cash[membership_table] where ms_id = '$ms_id' ");
    $ms_name = $row[ms_name];
}

if ($mi_day)
    $sql_search .= " and ml_day = '$mi_day' ";

if ($acc)
    $sql_search .= " and '$g4[time_ymdhis]' between ml_sdate and ml_edate ";

if ($stx) {
    $sql_search .= " and ( ";
    switch ($sfl) {
        case "mb_id" :
            $sql_search .= " (l.mb_id = '$stx') ";
            break;
        default : 
            $sql_search .= " ($sfl like '%$stx%') ";
            break;
    }
    $sql_search .= " ) ";
}

if ($date)
    $sql_search .= " and ml_datetime like '$date%' ";

if ($fr_date && $to_date)
    $sql_search .= " and ml_datetime between '$fr_date 00:00:00' and '$to_date 23:59:59' ";

//if ($rel_table && $rel_id) $sql_search .= " and rel_table = '$rel_table' and rel_id = '$rel_id'  ";

if (!$sst) {
    $sst  = "ml_datetime";
    $sod = "desc";
}
$sql_order = " order by $sst $sod ";

$sql = "select count(*) as cnt
        $sql_common
        $sql_search";
$row = sql_fetch($sql);
$total_count = $row[cnt];

$rows = $config[cf_page_rows];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함  */

$sql = "select *
        $sql_common
        $sql_search
        $sql_order
        limit $from_record, $rows ";
$result = sql_query($sql);

$write_pages = get_paging($rows, $page, $total_page, "$PHP_SELF?{$qstr}&page=");


$listall = "<a href='$_SERVER[PHP_SELF]'>처음</a>";

$g4[title] = "멤버쉽 내역";
include_once ("$g4[admin_path]/admin.head.php");

$colspan = 11;
?>

<script type="text/javascript" src="<?=$g4[path]?>/js/sideview.js"></script>
<script type="text/javascript">
var list_update_php = "";
var list_delete_php = "mw.membership.log.check.delete.php";
</script>

<!--
<link type="text/css" href="<?=$board_skin_path?>/mw.js/ui-lightness/jquery-ui-1.8.19.custom.css" rel="stylesheet" />
<script type="text/javascript" src="<?=$board_skin_path?>/mw.js/jquery-ui-1.8.19.custom.min.js"></script>
-->

<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>

<?
//==============================================================================
// jquery date picker
//------------------------------------------------------------------------------
// 참고) ie 에서는 년, 월 select box 를 두번씩 클릭해야 하는 오류가 있습니다.
//------------------------------------------------------------------------------
// jquery-ui.css 의 테마를 변경해서 사용할 수 있습니다.
// base, black-tie, blitzer, cupertino, dark-hive, dot-luv, eggplant, excite-bike, flick, hot-sneaks, humanity, le-frog, mint-choc, overcast, pepper-grinder, redmond, smoothness, south-street, start, sunny, swanky-purse, trontastic, ui-darkness, ui-lightness, vader
// 아래 css 는 date picker 의 화면을 맞추는 코드입니다.
?>
<style type="text/css">
<!--
.ui-datepicker { font:12px dotum; }
.ui-datepicker select.ui-datepicker-month, 
.ui-datepicker select.ui-datepicker-year { width: 70px;}
.ui-datepicker-trigger { margin:0 0 -5px 2px; }
-->
</style>
<script type="text/javascript">
/* Korean initialisation for the jQuery calendar extension. */
/* Written by DaeKwon Kang (ncrash.dk@gmail.com). */
jQuery(function($){
        $.datepicker.regional['ko'] = {
                closeText: '닫기',
                prevText: '이전달',
                nextText: '다음달',
                currentText: '오늘',
                monthNames: ['1월(JAN)','2월(FEB)','3월(MAR)','4월(APR)','5월(MAY)','6월(JUN)',
                '7월(JUL)','8월(AUG)','9월(SEP)','10월(OCT)','11월(NOV)','12월(DEC)'],
                monthNamesShort: ['1월','2월','3월','4월','5월','6월',
                '7월','8월','9월','10월','11월','12월'],
                dayNames: ['일','월','화','수','목','금','토'],
                dayNamesShort: ['일','월','화','수','목','금','토'],
                dayNamesMin: ['일','월','화','수','목','금','토'],
                weekHeader: 'Wk',
                dateFormat: 'yy-mm-dd',
                firstDay: 0,
                isRTL: false,
                showMonthAfterYear: true,
                yearSuffix: ''};
        $.datepicker.setDefaults($.datepicker.regional['ko']);

    $('#ml_edate').datepicker({
        showOn: 'button',
        buttonImage: 'img/calendar.gif',
        buttonImageOnly: true,
        buttonText: "달력",
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        yearRange: 'c-99:c+99'
    }); 
});
</script>
<?
//==============================================================================
?>

<table width=100%>
<form name=fsearch method=get>
<input type="hidden" name="ms_id" value="<?=$ms_id?>">
<tr>
    <td width=50% align=left>
        <?=$listall?> (건수 : <?=number_format($total_count)?>)
        <? if ($ms_id) echo "$ms_name "; else echo "멤버쉽 전체 "; ?>
        <? if ($mi_day) echo " {$mi_day}일 "; ?>
        <? if ($acc) echo " 현재 "; else echo " 누적 "; ?>
    </td>
    <td width=50% align=right>
        <select name=sfl class=cssfl>
            <option value='mb_id'>회원아이디</option>
            <option value='ml_sdate'>시작일</option>
            <option value='ml_edate'>종료일</option>
        </select>
        <input type=text name=stx class=ed required itemname='검색어' value='<?=$stx?>'>
        <input type=image src='<?=$g4[admin_path]?>/img/btn_search.gif' align=absmiddle></td>
</tr>
</form>
</table>

<form name=fpointlist method=post>
<input type=hidden name=sst   value='<?=$sst?>'>
<input type=hidden name=sod   value='<?=$sod?>'>
<input type=hidden name=sfl   value='<?=$sfl?>'>
<input type=hidden name=stx   value='<?=$stx?>'>
<input type=hidden name=page  value='<?=$page?>'>
<input type=hidden name=token value='<?=$token?>'>
<input type=hidden name=old_ms_id value='<?=$ms_id?>'>
<input type=hidden name=mi_day value='<?=$mi_day?>'>
<input type=hidden name=acc value='<?=$acc?>'>

<table width=100% cellpadding=0 cellspacing=1>
<colgroup width=30>
<colgroup width=120>
<colgroup width=80>
<colgroup width=''>
<colgroup width=50>
<colgroup width=50>
<colgroup width=70>
<colgroup width=70>
<!--<colgroup width=70>-->
<colgroup width=60>
<tr><td colspan='<?=$colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td><input type=checkbox name=chkall value='1' onclick='check_all(this.form)'></td>
    <td>멤버쉽</td>
    <td><?=subject_sort_link('mb_id')?>회원아이디</a></td>
    <td>별명</td>
    <td>기간</td>
    <td><?=$mw_cash[cf_cash_name]?></td>
    <td><?=subject_sort_link('ml_sdate', $qstr)?>시작일</a></td>
    <td><?=subject_sort_link('ml_edate', $qstr)?>종료일</a></td>
    <!--<td>결제일시</td>-->
    <td><a href="mw.membership.log.form.php<? if ($sfl=='mb_id' && $stx) echo "?mb_id=$stx";?>"><img src="<?=$g4[admin_path]?>/img/icon_insert.gif"></a></td>
</tr>
<tr><td colspan='<?=$colspan?>' class='line2'></td></tr>
<?
for ($i=0; $row=sql_fetch_array($result); $i++) 
{
    $sql1 = "select ms_name from $mw_cash[membership_table] where ms_id = '$row[ms_id]'";
    $row1 = sql_fetch($sql1);

    /*if ($row2[mb_id] != $row[mb_id])
    {
        $sql2 = " select mb_id, mb_name, mb_nick, mb_email, mb_homepage, mb_point from $g4[member_table] where mb_id = '$row[mb_id]' ";
        $row2 = sql_fetch($sql2);
    }*/
 
    $mb_nick = get_sideview($row[mb_id], $row[mb_nick], $row[mb_email], $row[mb_homepage]);

    $btn_mod = "<a href=\"mw.membership.log.form.php?w=u&ml_id=$row[ml_id]$qstr\"><img src=\"$g4[admin_path]/img/icon_modify.gif\"></a>";
    $btn_del = "<a href=\"javascript:del('mw.membership.log.update.php?w=d&ml_id=$row[ml_id]$qstr')\"><img src=\"$g4[admin_path]/img/icon_delete.gif\"></a>";

    $direct = $row[ml_direct] ? "√" : '';

    $list = $i%2;
    echo "
    <input type=hidden name=ml_id[$i] value='$row[ml_id]'>
    <tr class='list$list col1 ht center'>
        <td><input type=checkbox name=chk[] value='$i'></td>
        <td><a href='?ms_id=$row[ms_id]'>$row1[ms_name]</a></td>
        <td><a href='?sfl=mb_id&stx=$row[mb_id]'>$row[mb_id]</a></td>
        <td>$mb_nick</td>
        <td>$row[ml_day] 일</td>
        <td>$row[ml_cash] $mw_cash[cf_cash_unit]</td>
        <td>$row[ml_sdate]</td>
        <td>$row[ml_edate]</td>
        <!--<td>$row[ml_datetime]</td>-->
        <td> $btn_mod $btn_del </td>
    </tr> ";
} 

if ($i == 0)
    echo "<tr><td colspan='$colspan' align=center height=100 bgcolor=#ffffff>자료가 없습니다.</td></tr>";

echo "<tr><td colspan='$colspan' class='line2'></td></tr>";
echo "</table>";

$pagelist = get_paging($config[cf_write_pages], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&page=");
echo "<table width=100% cellpadding=3 cellspacing=1>";
echo "<tr><td width=50%>";
//echo "<input type=button class='btn1' value='선택삭제' onclick=\"btn_check(this.form, 'delete')\">";
?>
<select name="ms_id" onchange="btn_action(this.form, this.value)">
<option value="">== 선택 ==</option>
<?
$sql = " select * from $mw_cash[membership_table] ";
$qry = sql_query($sql);
while ($row = sql_fetch_array($qry)) {  ?>
<option value="<?=$row[ms_id]?>">선택 <?=$row[ms_name]?> 으로 이동</option>
<? } ?>
<option value="del"> 선택삭제 </option>
<option value="edate"> 종료일 변경 </option>
</select>
<input type="text" size="10" class="ed" id="ml_edate" name="ml_edate"/>
<?
echo "</td>";
echo "<td width=50% align=right>$pagelist</td></tr></table>\n";

if ($stx)
    echo "<script type='text/javascript'>document.fsearch.sfl.value = '$sfl';</script>\n";

if (strstr($sfl, "mb_id"))
    $mb_id = $stx;
else
    $mb_id = "";
?>
</form>

<script type="text/javascript"> document.fsearch.stx.focus(); </script>

<script type="text/javascript">
function btn_action(f, val)
{
    if (val == 'del') {
        btn_check(f, 'delete');
    }
    else if (val == 'edate') {
        btn_edate(f, val);
    }
    else {
        btn_move(f, val);
    }
}

function btn_edate(f)
{
    f.action = "mw.membership.log.check.edate.php";

    var chk = document.getElementsByName("chk[]");
    var bchk = false;

    for (i=0; i<chk.length; i++)
    {
        if (chk[i].checked)
            bchk = true;
    }

    if (!bchk) 
    {
        alert("변경할 자료를 하나 이상 선택하세요.");
        f.ms_id.value = '';
        return;
    }

    if (!$("#ml_edate").val()) {
        alert("종료일을 입력해주세요.");
        f.ms_id.value = '';
        $("#ml_edate").focus();
        return;
    }

    if (!confirm("선택한 자료의 종료일을 정말 변경 하시겠습니까?")) {
        f.ms_id.value = '';
        return;
    }

    f.submit();
}

function btn_move(f, ms_id)
{
    f.action = "mw.membership.log.check.update.php";

    var chk = document.getElementsByName("chk[]");
    var bchk = false;

    for (i=0; i<chk.length; i++)
    {
        if (chk[i].checked)
            bchk = true;
    }

    if (!bchk) 
    {
        alert("자료를 하나 이상 선택하세요.");
        f.ms_id.value = '';
        return;
    }

    if (!confirm("선택한 자료를 정말 이동 하시겠습니까?")) {
        f.ms_id.value = '';
        return;
    }

    f.submit();
}
</script>

<?
include_once ("$g4[admin_path]/admin.tail.php");
